﻿
using System;
using System.Collections.Generic;
namespace EasyComm.ServiceUnit
{
    public class SystemServiceUnitCollection : ThreadSafeCollection<SystemServiceUnit>
    {
        public virtual SystemServiceUnit this[Uri uri]
        {
            get
            {
                base.InnerLock.EnterReadLock();
                try
                {
                    foreach (SystemServiceUnit unit in this)
                    {
                        foreach (ServiceContract contract in unit.Contracts)
                        {
                            if (contract.ListenUri == uri)
                            {
                                return unit;
                            }
                        }
                    }
                    return null;
                }
                finally
                {
                    base.InnerLock.ExitReadLock();
                }
            }
        }

        public virtual List<SystemServiceUnit> this[ServiceContract contract]
        {
            get
            {
                base.InnerLock.EnterReadLock();
                try
                {
                    List<SystemServiceUnit> unitList = new List<SystemServiceUnit>();
                    foreach (SystemServiceUnit unit in this)
                    {
                        foreach (ServiceContract innerContract in unit.Contracts)
                        {
                            if (innerContract.Equals(contract))
                            {
                                unitList.Add(unit);
                            }
                        }
                    }
                    return unitList;
                }
                finally
                {
                    base.InnerLock.ExitReadLock();
                }
            }
        }
    }
}
